package com.aidong.pay.action;

import java.util.HashMap;
import java.util.Map;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.json.annotations.JSON;
import org.springframework.beans.factory.annotation.Autowired;

import com.aidong.pay.biz.OrderManagementBiz;
import com.aidong.pay.entity.TrnCampaignSignOrder;
import com.aidong.stadium.biz.MstScheduleBiz;
import com.aidong.util.bus.CodeConsts;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 预约、报名订单管理
 * @author user
 *
 */
@ParentPackage("json")
@Namespace("/")
public class OrderManagementAction implements ModelDriven<TrnCampaignSignOrder>{
	
	private TrnCampaignSignOrder orderManagement;
	
	@Override
	public TrnCampaignSignOrder getModel() {
		if(orderManagement == null){
			orderManagement = new TrnCampaignSignOrder();
		}
		return orderManagement;
	}

	private Map<String, Object> map;

	@JSON
	public Map<String, Object> getMap() {
		return map;
	}
	
	private OrderManagementBiz orderManagementBiz;
	private MstScheduleBiz aboutScheduleBiz; // 团操service
	
	@Autowired
	public void setOrderManagementBiz(OrderManagementBiz orderManagementBiz) {
		this.orderManagementBiz = orderManagementBiz;
	}
	/**
	 *  团操service
	 * @param aboutScheduleBiz
	 */
	@Autowired
	public void setAboutScheduleBiz(MstScheduleBiz aboutScheduleBiz) {
		this.aboutScheduleBiz = aboutScheduleBiz;
	}

	/**
	 * 申请退款
	 * @return
	 */
//	@Action(value="addapplyorderrefund",results={@Result(name="succ",type="json",params={"root","map"})})
//	public String addApplyOrderRefund(){
//		int orderId = orderManagement.getOrderId();
//		int subscribeId = orderManagement.getSubscribeId();
//		String refundCause = orderManagement.getRefundCause();
//		String applyType = orderManagement.getApplyType();
//		String price = orderManagement.getPrice();
//		int idongId = orderManagement.getIdongId();
//		String refundMark = orderManagement.getRefundMark();
//		map = new HashMap<String, Object>();
//		if(orderId > 0 && subscribeId > 0  && !"".equals(applyType) && applyType != null){
//			Map<String, Object> refundMap = new HashMap<String, Object>();
//			try {
//				refundMap.put("orderId", orderId);
//				refundMap.put("subscribeId", subscribeId);
//				refundMap.put("idongId", idongId);
//				refundMap.put("refundCause", refundCause);
//				refundMap.put("applyType", applyType);
//				refundMap.put("price", price);
//				refundMap.put("refundMark", refundMark);
//				
//				// 添加退款记录
//				orderManagementBiz.addApplyOrderRefund(refundMap);
//				
////				// 修改预约记录
////				Map<String, Object> appointMap = new HashMap<String, Object>();
////				appointMap.put("applyType", applyType);
////				appointMap.put("subscribeId", subscribeId);
////				appointMap.put("status", 1);
////				orderManagementBiz.editAppointStatus(appointMap);
////				
////				// 修改支付记录
//				Map<String, Object> payMap = new HashMap<String, Object>();
//				payMap.put("orderId", orderId);
//				payMap.put("status", 5);
//				payMap.put("processId", 6);
//				orderManagementBiz.editOrderPayStatus(payMap);
//				
//				map.put("code", 1);
//				map.put("msg", "已受理");
//				return "succ";
//			} catch (Exception e) {
//				e.printStackTrace();
//				map.put("code", 0);
//				map.put("msg", "系统异常");
//				return "succ";
//			}
//		}else{
//			map.put("code", 0);
//			map.put("msg", "参数缺失");
//			return "succ";
//		}
//	}
	
	/**
	 * 用户取消预约
	 * @return
	 */
	@Action(value="cancelreservation",results={@Result(name="succ",type="json",params={"root","map"})})
	public String cancelReservation(){
		int orderId = orderManagement.getOrderId();
		int subscribeId = orderManagement.getSubscribeId();
		String applyType = orderManagement.getApplyType();
		map = new HashMap<String, Object>();
		if(orderId > 0 && subscribeId > 0){
			try {
				// 修改预约状态
				Map<String, Object> appointMap = new HashMap<String, Object>();
				appointMap.put("applyType", applyType);
				appointMap.put("subscribeId", subscribeId);
				appointMap.put("status", 1);
				int cou=orderManagementBiz.getLuckyDrawBySubscribeId(appointMap);
				if(cou>0){
					map.put("code", 0);
					map.put("result", "该活动不能取消");
					return "succ";
				}
				int updateResult = orderManagementBiz.editAppointStatus(appointMap);
				
				//if (updateResult > 0 && applyType.equals("2")) {//取消课程预约
					//根据预约号查询课程ID
					//int autoId = aboutScheduleBiz.getScheduleAutoId(subscribeId);
					//aboutScheduleBiz.cutScheduleVersion(autoId);
				//}
				
				// 修改支付状态
				Map<String, Object> payMap = new HashMap<String, Object>();
				payMap.put("orderId", orderId);
				payMap.put("status", 0);
				payMap.put("processId", 5);
				orderManagementBiz.editOrderPayStatus(payMap);
				
				//恢复券的状态
				//改电影券改为未使用
				Map<String, Object> paraMap = new HashMap<String, Object>();
				paraMap.put("orderId", orderId);
				orderManagementBiz.setMovieTicketsUnuse(paraMap);
				
				//代金券改为未使用
				orderManagementBiz.setCouponsTicketsUnuse(paraMap);
				
				//设置兑换券未使用
				//orderManagementBiz.setExchangeTicketsUnuse(paraMap);
				
				//修改是否已报满的标记位
				orderManagementBiz.setActivityFlag(orderId);
				
				map.put("code", 1);
				map.put("result", "取消成功");
				return "succ";
			} catch (Exception e) {
				e.printStackTrace();
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		}else{
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
	}
	
	/**
	 * 删除订单、预约记录
	 * @return
	 */
	@Action(value="delorderpaystatus",results={@Result(name="succ",type="json",params={"root","map"})})
	public String delOrderPayStatus(){
		int orderId = orderManagement.getOrderId();
		int subscribeId = orderManagement.getSubscribeId();
		String applyType = orderManagement.getApplyType();
		map = new HashMap<String, Object>();
		if(orderId > 0 && subscribeId > 0){
			try {
				// 修改预约状态
				Map<String, Object> appointMap = new HashMap<String, Object>();
				appointMap.put("applyType", applyType);
				appointMap.put("subscribeId", subscribeId);
				orderManagementBiz.delAppointStatus(appointMap);
				
				// 修改支付状态
				Map<String, Object> payMap = new HashMap<String, Object>();
				payMap.put("orderId", orderId);
				orderManagementBiz.delOrderPayStatus(payMap);
				
				map.put("code", 1);
				map.put("result", "删除成功");
				return "succ";
			} catch (Exception e) {
				e.printStackTrace();
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		}else{
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
	}
	
	/**
	 * 活动订单详情
	 * @return
	 */
	@Action(value="getactiveorderdetail",results={@Result(name="succ",type="json",params={"root","map"})})
	public String getActiveOrderDetail() {
		int orderId = orderManagement.getOrderId();
		map = new HashMap<String, Object>();
		if(orderId > 0){
			try {
				Map<String, Object> resultMap = orderManagementBiz.getActiveOrderDetail(orderId);
				int orderType = Integer.parseInt(resultMap.get("orderType").toString());
				if (orderType == 6) {
					resultMap.put("pickTakeDate", resultMap.get("pickTakeDate"));
					if (resultMap.get("pickUpWay").equals("3")) {//虚拟码
						resultMap.put("tickNo", resultMap.get("movieTicketNum"));
						resultMap.remove("movieTicketNum");
					}
				}
				String ticketNum = resultMap.get("ticketNum") + "";
				if("null".equals(ticketNum) || ticketNum == null) {
					resultMap.put("ticketNum", "");
				}
				String ticketName = resultMap.get("ticketName") + "";
				if("null".equals(ticketName) || ticketName == null){
					resultMap.put("ticketName", "");
				}
				
				if ( ((Number) resultMap.get("actually")).doubleValue() < 0) {
					resultMap.put("actually", 0);
				}
				resultMap.put("freight", CodeConsts.Freight);// 运费--需要配置
				//resultMap.put("logisticsNum", "0000");//快递单号
				map.put("code", 1);
				map.put("result", resultMap);
				return "succ";
			} catch (Exception e) {
				e.printStackTrace();
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		}else{
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
	}
	
	/**
	 * 课程订单详情
	 * @return
	 */
	@Action(value="getcourseorderdetail",results={@Result(name="succ",type="json",params={"root","map"})})
	public String getCourseOrderDetail(){
		int orderId = orderManagement.getOrderId();
		map = new HashMap<String, Object>();
		if(orderId > 0){
			try {
				Map<String, Object> resultMap = orderManagementBiz.getCourseOrderDetail(orderId);
				map.put("code", 1);
				String ticketNum = resultMap.get("ticketNum") + "";
				if("null".equals(ticketNum) || ticketNum == null){
					resultMap.put("ticketNum", "");
				}
				String ticketName = resultMap.get("ticketName") + "";
				if("null".equals(ticketName) || ticketName == null){
					resultMap.put("ticketName", "");
				}
				if ( ((Number) resultMap.get("actually")).doubleValue() < 0) {
					resultMap.put("actually", 0);
				}
				map.put("result", resultMap);
				return "succ";
			} catch (Exception e) {
				e.printStackTrace();
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		}else{
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
	}
	
	/**
	 * 餐饮订购详情
	 * @return
	 */
	@Action(value="getfoodorderdetail",results={@Result(name="succ",type="json",params={"root","map"})})
	public String getFoodOrderDetail() {
		int orderId = orderManagement.getOrderId();
		map = new HashMap<String, Object>();
		if(orderId > 0){
			try {
				Map<String, Object> resultMap = orderManagementBiz.getFoodOrderDetail(orderId);
				resultMap.put("pickTakeDate",  resultMap.get("buyDate") + " " + resultMap.get("formDate") + "-" + resultMap.get("toDate"));
				//buyDate 配送日期
				resultMap.remove("buyDate");
				resultMap.remove("formDate");
				resultMap.remove("toDate");
				String ticketNum = resultMap.get("ticketNum") + "";
				if("null".equals(ticketNum) || ticketNum == null){
					resultMap.put("ticketNum", "");
				}
				String ticketName = resultMap.get("ticketName") + "";
				if("null".equals(ticketName) || ticketName == null){
					resultMap.put("ticketName", "");
				}
				if ( ((Number) resultMap.get("actually")).doubleValue() < 0) {
					resultMap.put("actually", 0);
				}
				//resultMap.put("logisticsNum", "0000");//物流单号  待设置 CodeConsts.Freight
				resultMap.put("freight", CodeConsts.Freight);
				map.put("code", 1);
				map.put("result", resultMap);
				return "succ";
			} catch (Exception e) {
				e.printStackTrace();
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		}else{
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
	}
	
	
	/**
	 * 删除订单(因为微信支付失败，所以删除之后重新下单)
	 * @return
	 */
	@Action(value="delorder",results={@Result(name="succ",type="json",params={"root","map"})})
	public String delorder(){
		int orderId = orderManagement.getOrderId();
		map = new HashMap<String, Object>();
		if(orderId > 0){
			try {
				
				// 修改支付状态
				Map<String, Object> payMap = new HashMap<String, Object>();
				payMap.put("orderId", orderId);
				orderManagementBiz.delOrderPayStatus(payMap);

				orderManagementBiz.setCouponsTicketsUnuse(payMap);
				orderManagementBiz.setDiscountTicketsUnuse(payMap);
				
				map.put("code", 1);
				map.put("result", "删除成功");
				return "succ";
			} catch (Exception e) {
				e.printStackTrace();
				map.put("code", 0);
				map.put("msg", "系统异常");
				return "succ";
			}
		}else{
			map.put("code", 0);
			map.put("msg", "参数缺失");
			return "succ";
		}
	}
}